<template>
  <div class="articles">
    <van-cell
      v-for="(item, i) in list"
      :key="i"
      :to="{ name: 'article', params: { id: item.id, article: item } }"
    >
      <div class="cell-header">
        <van-image
          width="30"
          height="30"
          :src="
            item.original_status.user.photo_domain +
            item.original_status.user.profile_image_url.split(',')[0]
          "
        />
        <div class="right-header">
          <div class="author">{{ item.original_status.user.screen_name }}</div>
          <div class="pubtime">{{ item.original_status.timeBefore }}</div>
        </div>
      </div>
      <p v-html="item.original_status.description"></p>
      <div class="cell-footer">
        <div class="item">
          <van-icon name="share-o" />
          <div class="num">{{ item.original_status.retweet_count }}</div>
        </div>
        <div class="item">
          <van-icon name="chat-o" />
          <div class="num">{{ item.original_status.reply_count }}</div>
        </div>
        <div class="item">
          <van-icon name="good-job-o" />
          <div class="num">{{ item.original_status.like_count }}</div>
        </div>
      </div>
    </van-cell>
    <div v-show="loading" class="finished">加载中</div>
    <div v-show="finished" class="finished">已加载完毕</div>
  </div>
</template>

<script>
import * as api from "@/api/index";
export default {
  data() {
    return {
      page: 1,
      list: [],
      finished: false,
      loading: false,
    };
  },
  methods: {
    async getList() {
      if (this.finished == true) {
        return;
      }
      this.loading = true;
      let result = await api.getArticles({ page: this.page });
      this.loading = false;
      console.log(result);
      if (result.result && result.result.indexOf("已加载") !== -1) {
        this.finished = true;
      } else {
        this.list = this.list.concat(result.items);
        this.page++;
      }
    },
    debounce(fn, delay) {
      let timer = null;
      return function () {
        if (timer) {
          clearTimeout(timer);
          timer = setTimeout(fn, delay);
        } else {
          timer = setTimeout(fn, delay);
        }
      };
    },
  },
  async mounted() {
    this.getList();
    // 只有当滚动停下来，0.25s之后才会触发
    let timer = null;
    window.addEventListener(
      "scroll",
      this.debounce(() => {
        let scrollHeight = document.documentElement.scrollHeight;
        let scrollTop = document.documentElement.scrollTop;
        let innerHeight = window.innerHeight;
        console.log(scrollTop);
        if (scrollHeight - scrollTop - innerHeight < 50) {
          this.getList();
        }
      }, 250)
    );
  },
};
</script>

<style scoped lang="less">
.cell-header {
  display: flex;
  height: 1rem;
  .right-header {
    font-size: 0.3rem;
    line-height: 0.4rem;
    padding-left: 0.2rem;
    .pubtime {
      color: #999;
    }
  }
}

.cell-footer {
  display: flex;
  height: 0.5rem;
  align-items: center;
  .item {
    display: flex;
    font-size: 0.3rem;
    width: 1.5rem;
    height: 0.5rem;
    align-items: center;
    .num {
      padding: 0 0.1rem;
    }
  }
}
.finished,
.loading {
  text-align: center;
  color: #bbb;
  height: 1rem;
  line-height: 1rem;
  font-size: 0.4rem;
  background-color: #efefef;
}
</style>
